package com.arpa.ntocc.basic.api.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.basic.api.domain.dto.TableBackupConfigDTO;
import com.arpa.ntocc.basic.api.domain.entity.TableBackupConfig;
import com.arpa.ntocc.basic.api.domain.vo.TableBackupConfigVO;
import com.arpa.ntocc.basic.api.service.ITableBackupConfigService;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @Title 表数据备份配置 Controller
 *
 * @ClassName TableBackupConfigController
 * @Author HSL_WITHER
 * @Date 2021_06_08
 * @version 1.1.210608
 *
 */

@Api(tags = "表数据备份配置")
@RestController
@RequestMapping(value = "/tableBackupConfig", produces = "application/json;charset=UTF-8")
@Log4j2(topic = "business")
public class TableBackupConfigController extends BaseController {

    @Autowired
    private ITableBackupConfigService iTableBackupConfigService;

    /**
     * 获取 表数据备份配置 列表
     *
     * @param tableBackupConfigDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 表数据备份配置 列表", notes = "获取 表数据备份配置 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = TableBackupConfigVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(TableBackupConfigDTO tableBackupConfigDTO, HttpServletRequest request) {
        try {
            TableBackupConfigVO tableBackupConfigVOSum = iTableBackupConfigService.queryListSum(tableBackupConfigDTO);
            List<TableBackupConfigVO> tableBackupConfigVOList = iTableBackupConfigService.queryList(tableBackupConfigDTO);

            Page<TableBackupConfigVO> returnPage = new Page<>(tableBackupConfigDTO.getPageNum(), tableBackupConfigDTO.getPageSize());
            returnPage.setRecords(tableBackupConfigVOList);
            returnPage.setTotal(tableBackupConfigVOSum.getTotal());
            returnPage.setSumObject(tableBackupConfigVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error("表数据备份配置列表查询异常", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 表数据备份配置
     *
     * @param entity  TableBackupConfig
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 表数据备份配置 ", notes = "添加 表数据备份配置 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(TableBackupConfig entity, HttpServletRequest request) {
        try {
            iTableBackupConfigService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error("表数据备份配置添加异常", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取表数据备份配置详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取表数据备份配置详情", notes = "获取表数据备份配置详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "表数据备份配置code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = TableBackupConfig.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result tableBackupConfig(@PathVariable String code, HttpServletRequest request) {
        try {
            TableBackupConfig entity = iTableBackupConfigService.getOne(new QueryWrapper<TableBackupConfig>().lambda().eq(TableBackupConfig::getCode, code));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error("表数据备份配置获取详情异常", e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改表数据备份配置
     *
     * @param entity  表数据备份配置
     * @param request
     * @return
     */
    @ApiOperation(value = "修改表数据备份配置", notes = "修改表数据备份配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(TableBackupConfig entity, HttpServletRequest request) {
        if (StringUtils.isBlank(entity.getCode())) {
            return Result.error("code不能为空");
        }
        int updateNum = iTableBackupConfigService.updateByCode(entity);
        if (updateNum == 0) {
            return Result.error("没有更新数据！code:" + entity.getCode());
        } else {
            return Result.ok("修改成功,更新" + updateNum + "条数据！");
        }
    }

    /**
     * 删除 表数据备份配置
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 表数据备份配置 ", notes = "删除 表数据备份配置 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iTableBackupConfigService.remove(new QueryWrapper<TableBackupConfig>().lambda().in(TableBackupConfig::getCode, codes));
            return Result.ok();
        } catch (Exception e) {
            log.error("表数据备份配置删除异常", e);
            return Result.error(e.getMessage());
        }
    }

}
